<?php

/**
 * 用户相关接口
 */
class userController extends base
{

    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 添加及修改设备关联用户信息
     */
    public function dosetUserInfo()
    {
        $nickname = common::request('nickname', 'P', NULL, TRUE);
        $height = common::request('height', 'P');
        $weight = common::request('weight', 'P');
        $gender = intval(common::request('gender', 'P', 1));
        $age = common::request('age', 'P', '18');
        $gender = empty($gender) ? 1 : $gender;
        $nickname = trim($nickname);
        if($this->vn >= 1400 || ($this->vn >= 1200 && $this->projectId == 2))
        {
            $birth_year = common::request('birth_year', 'P');
            $birth_month = common::request('birth_month', 'P',1);
            $birth_day = common::request('birth_day', 'P',1);
            $age = date("Y") - $birth_year;
            $occupation = common::request('occupation', 'P');
            $occupa_type = common::request('occupa_type', 'P');
            $province = common::request('province', 'P');
            $city = common::request('city', 'P');
            $signature = common::request('signature', 'P');
            if(mb_strlen($signature,'utf8') > 30)
            {
                $_ENV['error']->getErrors(702);
                exit;
            }
        }
        $newUserInfo = array();
        if ($height && $weight && $gender && $age)
        {
            if ($height >= 100 && $height < 300 && $weight >= 20 && $weight <= 200 && $age >=8 && $age <= 100)
            {
                $newUserInfo = array(
                    'height' => $height,
                    'weight' => $weight,
                    'age' => $age,
                    'gender' => $gender,
                    'is_defaultData' => 2
                );
                if($this->vn >= 1400 || ($this->vn >= 1200 && $this->projectId == 2))
                {
                    if(empty($birth_year)||empty($birth_month)||empty($birth_day))
                    {
                        $_ENV['error']->getErrors(101);
                        exit;
                    }
                    $newUserInfo['birthday'] = $birth_year.'-'.$birth_month.'-'.$birth_day;
                    $newUserInfo['occupation'] = $occupation;
                    $newUserInfo['occupa_type'] = $occupa_type;
                    $newUserInfo['constellation'] = $this->getConstellation($birth_month, $birth_day);
                    $newUserInfo['province'] = $province;
                    $newUserInfo['city'] = $city;
                    $newUserInfo['signature'] = $signature;
                }
                if(!empty($this->uid))
                {
                    if(empty($nickname))
                    {
                        $_ENV['error']->getErrors(101);
                        exit;
                    }
                    $checkNickname = $this->checkNickname($nickname);
                    if($checkNickname == -3)
                    {
                        $_ENV['error']->getErrors(503);
                        exit;
                    }
                    else
                    {
                        if(isset($_FILES['header']))
                        {
                            $imgName = md5($this->uid.common::getHashStr(6).time());
                            $header = $this->load('qiniu')->uploadQiNiuImg('header', $imgName);
                            $newUserInfo['header'] = $this->load('qiniu')->qiniuPersist($header, '?imageMogr2/thumbnail/180x180');
                            $newUserInfo['header_source'] = $header;
                        }
                        $newUserInfo['nickname'] = $nickname;

                        $newUserInfo && $this->load('client')->setUserInfo($this->uid, $newUserInfo);
                        $this->result['result'] = $_ENV['client']->getUserInfo($this->uid);
                    }
                    /*
                    else
                    {
                        $errorId = 100;

                        switch ($checkNickname) {
                            case -1:
                                $errorId = 501;
                                break;

                            case -2:
                                $errorId = 502;
                                break;

                            case -3:
                                $errorId = 503;
                                break;
                        }

                        $_ENV['error']->getErrors($errorId);
                        exit;
                    }
                    */
                }
                else
                {
                    if($this->vn < '1100')
                    {
                        $header && $newUserInfo['header'] = $header;
                    }
                    $this->load('client')->saveVisitorInfo($this->vid, $newUserInfo);
                    $data = $this->load('client')->getVisitorInfo($this->vid);
                    $data['header'] = $this->vn < '1100' ? $data['header'] : '';
                    $this->result['result'] = $data;
                }
            }
            else
            {
                $_ENV['error']->getErrors(701);
                exit;
            }
        }
        else
        {
            $_ENV['error']->getErrors(101);
            exit;
        }
    }
    /**
     * 获取自己/他人个人资料
     */
    public function dogetUserCenterInfo(){
        $uid = common::request('uid','G');
        $add_type = common::request('type', 'G');
        empty($uid) && $uid = $this->uid;
        if(empty($uid))
        {
            $_ENV['error']->getErrors(101);
            exit;
        }
        $userInfo = $this->load('client')->getUserInfo($uid);
        if(empty($userInfo)){
            $this->result['result'] = $userInfo;
            exit;
        }
        $user_mod = $this->load('user');
        if($uid != $this->uid)
        {
            $userInfo['is_thumb'] = $this->redis->hexists(REDIS_PREFIX . 'userCenterLight_' . $uid, $this->uid) ? 1 : 0;
            if($this->vn >=2100)
            {
                $privacy = $user_mod->getPrivacy($uid);
                foreach ($privacy as $key => $v) {
                    if(empty($v)) {
                        switch ($key) {
                            case 'age':
                                if(isset($userInfo['age'])) unset($userInfo['age']);
                                if(isset($userInfo['birth_year'])) unset($userInfo['birth_year']);
                                break;
                            case 'location':
                                if(isset($userInfo['province'])) unset($userInfo['province']);
                                if(isset($userInfo['city'])) unset($userInfo['city']);
                                break;
                            case 'constellation':
                                if(isset($userInfo['constellation'])) unset($userInfo['constellation']);
                                if(isset($userInfo['birth_month'])) unset($userInfo['birth_month']);
                                if(isset($userInfo['birth_day'])) unset($userInfo['birth_day']);
                                break;
                            default:
                                if(isset($userInfo[$key])) unset($userInfo[$key]);
                                break;
                        }
                    }
                }
            }
            $userInfo['friendstatus'] = $this->load('friends')->getFriendStatus($this->uid, $uid);
        }
        if(isset($userInfo['privacy'])) unset($userInfo['privacy']);
        //最新徽章
        $userInfo['latestMedal'] = $this->load('medal')->getLatestMedals($uid);

        unset($userInfo['pwd']);
        //统计信息
        $userInfo['medal_count'] = $this->load('medal')->getMedalCount($uid);

        //待付订单数
        $userInfo['pending_order_count'] = $this->load('order')->getCountOrderByUid($uid,1);

        //跑友秀图片
        $post_thumb_img = [];
        $bbs_mod = $this->load('bbs');
        if($news_ids = $bbs_mod->getUserPostList('showtime',$uid,0,'next',20)){
            foreach($news_ids as $news_id){
                $postInfo = $bbs_mod->getPostInfo($news_id);
                $post_thumb_img = array_merge($post_thumb_img,$postInfo['thumb_img']);
            }
            if(count($post_thumb_img)>=4){
                $post_thumb_img = array_slice($post_thumb_img,0,4);
            }
        }

        $userInfo['post_thumb_img'] = $post_thumb_img;

        //当描述是里程的时候
        $lastRunDesc = $userInfo['latestRunRecord']['description'];
        if(is_numeric($lastRunDesc)){
            $latestRunWords = $this->config['latestRunWords'];
            $timediff = common::timediff(time() - $userInfo['latestRunRecord']['run_time'],'run');
            $userInfo['latestRunRecord']['description'] = sprintf($latestRunWords,$timediff,$lastRunDesc);
        }

        //banner
        $res = $this->load('banner')->getUserBanner();
        $banner = [];
        $now = time();
        foreach($res as $row){
            if(@$row['startTime'] && strtotime($row['startTime']) > $now){
                continue;
            }
            if(@$row['endTime'] && strtotime($row['endTime']) < $now){
                continue;
            }
            $banner[] = $row;
        }
        $userInfo['banner'] = $banner;
        
        //证书数
        $activity_mod = $this->load('groupActivity');
        $certificate_count = $activity_mod->getCertificateNo($uid);
        $old_count = $user_mod->getCertificateCount($uid);
        if($uid == $this->uid && $old_count < $certificate_count){
            $userInfo['certificate_notify'] = 1;
            $user_mod->setCertificateNotify($uid,1);
            $user_mod->setCertificateCount($uid,$certificate_count);
        }
        $userInfo['certificate_count'] = $certificate_count;
        $certificates = $activity_mod->getCertificates($uid,0,'next',2);
        if($certificates){
            $no = $activity_mod->getCertificateNo($uid,$certificates[0]['id']);
            foreach($certificates as $k=>&$cer){
                $cer['no'] = $no--;
                $cer['actInfo'] = $activity_mod->getActByAid($cer['aid']);
            }
        }
        $userInfo['latest_certificates'] = $certificates;

        //新增最佳成绩对应的跑步id   v_2.8.9
        if(!empty($userInfo['best_record'])){
            $userInfo['best_record'] = json_decode($userInfo['best_record']);
        }else{
            $userInfo['best_record'] = (object)[];
        }

        //个人中心导航   v_2.8.9
        $userInfo['channels'] = $this->load('client')->getUserChannel();
        if(!empty($userInfo['channels'])){
            $userInfo['channels'] = array_values($userInfo['channels']);
        }
        
        $this->result['result'] = $userInfo;
    }
    
    /*
     * 证书馆
     */
    public function docertificateList(){
        $uid = common::request('uid','G');
        $id = common::request('id','G','0');
        $direc = common::request('direc','G','next');
        $offset = common::request('offset','G','20');
        
        if(empty($uid)){
            $uid = $this->uid;
        }
        
        if(empty($uid))
        {
            $_ENV['error']->getErrors(101);
            exit;
        }
        
        $userInfo = $this->load('client')->getUserInfo($uid);
        if(empty($userInfo)){
            $_ENV['error']->getErrors(101);
            exit;
        }
        
        $activity_mod = $this->load('groupActivity');
        $act_type = $this->afterVersion('2.9.1','2.9.1') ? null : 0;
        $certificates = $activity_mod->getCertificates($uid,$id,$direc,$offset,$act_type);
        $match_mod = $this->load('match');
        $no = empty($certificates) ? 0 : $activity_mod->getCertificateNo($uid,$certificates[0]['id']);
        foreach($certificates as $k=>&$cer){
            $cer['no'] = $no--;
            $cer['actInfo'] = $cer['act_type']==0 ? $activity_mod->getActByAid($cer['aid']) : $match_mod->getMatchInfo($cer['aid']);
        }
        if($uid == $this->uid){
            $this->load('user')->setCertificateNotify($uid,0);
        }
        
        //新增最佳成绩对应的跑步id   v_2.8.9
        if(!empty($userInfo['best_record'])){
            $userInfo['best_record'] = json_decode($userInfo['best_record']);
        }else{
            $userInfo['best_record'] = (object)[];
        }
        
        $result['user'] = $userInfo;
        $result['certificate_count'] = $activity_mod->getCertificateNo($uid);
        $result['certificates'] = $certificates;
        $result['banner'] = [];
        $this->result['result'] = $result;
    }
    
    /**
     * 绑定用户
     */
    public function dobindSocialUser()
    {
        $openid = common::request('open_id', 'P');
        $access_token = common::request('access_token', 'P');
        $expire = common::request('expire', 'P');
        $type = common::request('type','P');
        $union_id = common::request('union_id','P', 0);
        $nickname = common::request('nickname', 'P', NULL, TRUE);
        $header = common::request('profile', 'P');
        $gender = common::request('gender', 'P',2);
        empty($gender) && $gender = 1;
        $packageChannel  = common::request('packageChannel', 'R', '');
        $fromChannel = trim($packageChannel);

        $bindInfo = compact('nickname', 'header', 'gender', 'fromChannel');
        if ($openid && $access_token && $expire >= 0 && $type)
        {
            $user_data = $this->load('social')->bindUser($openid, $access_token, $expire, $type, $union_id, $bindInfo);

            $this->result['result'] = $user_data;
            $this->result['is_old'] = empty($user_data['total_elapsedtime']) ? 0 : 1;

            // 设置Cookie信息
            if(!empty($user_data['token']))
            {
                setcookie('joggers_u', $user_data['token'], time() + 864000 * 365 * 10, '/', '.hupu.com', FALSE, TRUE);
                setcookie('joggers_u', $user_data['token'], time() + 864000 * 365 * 10, '/', '.ijoggers.com', FALSE, TRUE);
            }

        } else
        {
            $_ENV['error']->getErrors(101);
            exit();
        }
    }



    /**
     * 解除绑定用户
     */
    public function dounBindSocialUser()
    {
        $channel = common::request('type','P');
        if($channel && $this->uid)
        {
            $token = $this->load('social')->unBindUser($channel, $this->uid);

            $user_data = $this->load('client')->getUserInfo($this->uid);
            $user_data['token'] = $token;
            $this->result['result'] = $user_data;
        }
        else
        {
            $_ENV['error']->getErrors(101);
            exit();
        }

    }
    /**
     * 修改头像
     */
    public function doupdateHeader()
    {
        $uid = $this->uid;
        $userInfo = $this->load('client')->getUserInfo($uid);
        if(!$uid || empty($userInfo))
        {
            $_ENV['error']->getErrors(201);
            exit;
        }

        $header = $userInfo['header'];
        $header_source = $userInfo['header_source'];

        $newUserInfo = array('uid' => $uid, 'header_source' => & $header_source, 'header' => & $header);

        if(isset($_FILES['header']))
        {
            $imgName = md5($uid.common::getHashStr(6).$this->time);

            $newHeaderSource = $this->load('qiniu')->uploadQiNiuImg('header', $imgName);

            if($newHeaderSource)
            {
                $header_source = $newHeaderSource;
                $newHeader = $this->load('qiniu')->qiniuPersist($newHeaderSource, '?imageMogr2/thumbnail/180x180');
                $newHeader && $header = $newHeader;
            }
        }

        if($this->load('client')->setUserInfo($uid, $newUserInfo))
        {
            unset($newUserInfo['uid']);
            $this->result['result'] = $newUserInfo;
        }
        else
        {
            $this->result['result'] = array('header_source' => $userInfo['header_source'], 'header' => $userInfo['header']);
        }

    }
    /**
     * 检测昵称是否已可以使用
     */
    public function docheckNickname()
    {
        $nickname = common::request('nickname', 'G', NULL, TRUE);
        $uid = $this->uid;
        if(!$uid)
        {
            $_ENV['error']->getErrors(201);
            exit;
        }

        $is_legal = 1;

        $checkNickname = $this->checkNickname($nickname, $uid);
        if ($checkNickname !== 1) // 昵称重复
        {
            $is_legal = 0;
        }
        $this->result['result']['is_legal'] = $is_legal;
    }
    /*
     * 注册绑定社交用户
     */
    public function dofirstBindSocialUser()
    {
        $openid = common::request('open_id', 'P');
        $access_token = common::request('access_token', 'P');
        $expire = common::request('expire', 'P');
        $type = common::request('type','P');
        $union_id = common::request('union_id','P', 0);
        $nickname = common::request('nickname', 'P', NULL, TRUE);
        $header = common::request('profile', 'P');
        $gender = common::request('gender', 'P', 0);
        $height = common::request('height', 'P', 170);
        $weight = common::request('weight', 'P', 60);
        $age    = common::request('age', 'P');
        $age = ($age >100|| $age <8) ? 18: $age;
        empty($gender) && $gender = 1;
        $packageChannel  = common::request('packageChannel ', 'G', '');
        $fromChannel = trim($packageChannel);

        $bindInfo = compact('nickname', 'header', 'gender', 'height', 'weight', 'fromChannel');
        if($this->vn>= 1400 || ($this->vn >= 1200 && $this->projectId == 2))
        {
            $birth_year = common::request('birth_year', 'P', date("Y")-18);
            $birth_month= common::request('birth_month', 'P',1);
            $birth_day  = common::request('birth_day', 'P',1);
            $age = date("Y") - $birth_year;
            if($age > 100 || $age <8)
            {
                $birth_year = date("Y") -18;
                $age = 18;
            }
            $birth_month >12 && $birth_month = 1;
            $birth_day >31 && $birth_day = 1;
            $birthday = $birth_year.'-'.$birth_month.'-'.$birth_day;
            $bindInfo['age'] = $age;
            $bindInfo['birthday'] = $birthday;
        }
        if ($openid && $access_token && $expire >=0 && $type)
        {
            $user_data = $this->load('social')->bindUser($openid, $access_token, $expire, $type, $union_id, $bindInfo);
            $this->result['result'] = $user_data;
            $this->result['is_old'] = empty($user_data['total_elapsedtime']) ? 0 : 1;
        } else
        {
            $_ENV['error']->getErrors(101);
            exit();
        }
    }
    /*
     * 获取IOS 推送消息 token
     */
    public function dogetPushToken()
    {
        $push_token = common::request('push_token', 'G','');

        if(empty($push_token) || $this->projectId == 1 || empty($this->cid))
        {
            $_ENV['error']->getErrors(101);
            exit;
        }
        $this->result['result']['is_success'] = $this->load('client')->saveIOSPushToken($this->cid, $push_token);
    }
    /**
     * 获取星座
     */
    private function getConstellation($month =1, $day=1)
    {
        $month = intval($month) * 100;
        $day = intval($day);
        $value = $month + $day;
        $constellation = '双鱼座';
        switch ($value) {
            case $value >=321 && $value <= 419:
                $constellation = '白羊座';
                break;
            case $value >=420 && $value <= 520:
                $constellation = '金牛座';
                break;
            case $value >=521 && $value <= 621:
                $constellation = '双子座';
                break;
            case $value >=622 && $value <= 722:
                $constellation = '巨蟹座';
                break;
            case $value >=723 && $value <= 822:
                $constellation = '狮子座';
                break;
            case $value >=823 && $value <= 922:
                $constellation = '处女座';
                break;
            case $value >=923 && $value <= 1023:
                $constellation = '天秤座';
                break;
            case $value >=1024 && $value <= 1122:
                $constellation = '天蝎座';
                break;
            case $value >=1123 && $value <= 1221:
                $constellation = '射手座';
                break;
            case $value >=1222 || $value <= 119:
                $constellation = '摩羯座';
                break;
            case $value >=120 && $value <= 218:
                $constellation = '水瓶座';
                break;
            default:
                $constellation = '双鱼座';
                break;
        }
        return $constellation;
    }
    /*
     * 为别人点赞
     */
    public function dothumbsUserUp()
    {
        if(!$this->uid)
        {
            $_ENV['error']->getErrors(201);
            exit;
        }

        $uid = common::request('uid', 'G','');
        if(empty($uid) || $this->uid == $uid)
        {
            $_ENV['error']->getErrors(101);
            exit;
        }
        if ($this->redis->hexists(REDIS_PREFIX . 'userCenterLight_' . $uid, $this->uid))
        {
          $_ENV['error']->getErrors(703);
          exit();
        }
        $this->result['result'] = $_ENV['client']->thumbsUserUp($this->uid, $uid);
    }
    /*
     * 获取某用户的被赞列表
     */
    public function dogetThumbsList()
    {
        if(empty($this->uid))
        {
            $_ENV['error']->getErrors(201);
            exit;
        }
        $uid = common::request('uid', 'G');
        $record_id = common::request('record_id', 'G',0);
        $direc = common::request('direc', 'G','next');
        $offset = common::request('offset', 'G',20);
        empty($uid) && $uid = $this->uid;

        $this->result['result'] = $_ENV['client']->getThumbsList($uid, $record_id, $direc, $offset);
    }
    /*
     * 获取用户正能量变动记录
     */
    public function doenergyRecord()
    {
        $record_id = common::request('record_id', 'G',0);
        $direc = common::request('direc', 'G','next');
        $offset = common::request('offset', 'G',20);
        $uid = $this->uid;
        if(empty($uid))
        {
            $_ENV['error']->getErrors(201);
            exit;
        }

        $this->result['result'] = $_ENV['client']->getenergyRecord($uid, $record_id, $direc, $offset);
    }
    /*
     * 获取个人中心背景图列表
     */
    public function doUCenterBgList()
    {
        $uid = common::request('uid', 'G');

        $this->result['result'] = $_ENV['client']->getUCenterBgList();
    }
    /**
     * 设置个人中心背景图
     */
    public function dosetUcenterBg()
    {
        if(empty($this->uid))
        {
            $_ENV['error']->getErrors(201);
            exit;
        }
        $background = common::request('background_url', 'P');
        if(empty($background))
        {
            $imgName = md5($this->uid.common::getHashStr(6).time());
            $background = $this->load('qiniu')->uploadQiNiuImg('custom_img', $imgName);
        }
        $this->result['result'] = $_ENV['client']->setMycenterBg($this->uid, $background);
    }
    /**
     * 分享跑步记录成功获取正能量
     */
    public function doshareRunRecordSuccess()
    {
        if(empty($this->uid))
        {
            $_ENV['error']->getErrors(201);
            exit;
        }
        $order = common::request('order', 'G');

        $this->result['result'] = $_ENV['client']->shareRunGetEnergy($this->uid, $order);
    }
    /**
     * 获取用户正能量与等级
     */
    public function dogetUserLevelEnergy()
    {
        if(empty($this->uid))
        {
            $_ENV['error']->getErrors(201);
            exit;
        }
        $total_energy = $this->userInfo['total_energy'];
        $levelInfo = $_ENV['client']->getUserLevelInfo($this->uid);
        $NewFriendMess = $this->redis->hget(REDIS_PREFIX.'IsNewFriends', $this->uid) ? 1:0;
        $this->result['result'] = array('total_energy' => $total_energy, 'levelInfo' => $levelInfo,'NewFriendMess'=>$NewFriendMess);
    }
    /**
     * 侧边栏数据
     */
    public function dosidebarInfo()
    {
        $uid = $this->uid;
        if(empty($uid))
        {
            $_ENV['error']->getErrors(201);
            exit;
        }
        $total_energy = $this->userInfo['total_energy'];
        $levelInfo = $_ENV['client']->getUserLevelInfo($uid);
        $newFriends = $this->redis->hget(REDIS_PREFIX.'IsNewFriends', $uid) ? 1:0;
        $newMsg = $newFriends + $this->redis->ZCARD(REDIS_PREFIX.'QuitGNotice_'.$uid) + $this->redis->ZCARD(REDIS_PREFIX.'GInvite_'.$uid);
        $newMsg = $newMsg > 0 ? 1 : 0;
        $this->result['result'] = compact('total_energy','levelInfo', 'newMsg');
    }
    /**
     * 用户跑步等级列表
     */
    public function dorunLevelList()
    {
        if(empty($this->uid))
        {
            $_ENV['error']->getErrors(201);
            exit;
        }
        $this->result['result']['levelInfo'] = $_ENV['client']->getUserLevelInfo($this->uid);
        $this->result['result']['level_upgrade'] = $_ENV['client']->getRunLevelList($this->uid);
    }
    /**
     * 是否接收推送消息
     */
    public function dogetPushMes()
    {
        $receive = common::request('receive', 'G');
        $receive = $receive != 0 ? 1 : 0;
        $cid = $this->cid;

        $res = 1;
        if($cid)
        {
            $res = $this->load('client')->setPushMes($cid, $receive);
        }

        $this->result['result']['receive'] = $res;
    }
    /**
     * 搜索用户
     */
    public function dosearchUser()
    {
        $nickname = common::request('nickname', 'G');
        if(empty($nickname))
        {
            $_ENV['error']->getErrors(101);
            exit;
        }

        $this->load('friends');

        if($this->vn >= 2400)
        {
            $p = common::request('p', 'G', 1);
            $result = $this->load('user')->searchUsers($nickname, $p);
            if($result)
            {
                $uid = $this->uid;
                foreach ($result as & $v)
                {
                    $v['status'] = $_ENV['friends']->getFriendStatus($uid, $v['uid']);
                }
            }
            $this->result['result'] = $result;
        }
        else
        {
            $this->result['result'] = $_ENV['friends']->searchUsers($nickname);
        }
    }
    /**
     * 发送添加好友请求
     */
    public function dorequestAddFriend()
    {
        $uid = common::request('uid', 'G');
        $add_type = common::request('add_type', 'G');
        empty($add_type) && $add_type = 0;
        if(empty($this->uid))
        {
            $_ENV['error']->getErrors(201);
            exit;
        }
        if(empty($uid) ||$uid == $this->uid)
        {
            $_ENV['error']->getErrors(101);
            exit;
        }
        $this->result['result'] = $this->load('friends')->requestAddFriend($this->uid, $uid,$add_type);
    }
    /**
     * 同意好友申请
     */
    public function doconfirmAddFriend()
    {
        $uid = common::request('uid', 'G');
        empty($add_type) && $add_type = 0;
        if(empty($this->uid))
        {
            $_ENV['error']->getErrors(201);
            exit;
        }
        if(empty($uid))
        {
            $_ENV['error']->getErrors(101);
            exit;
        }
        $this->result['result'] = $this->load('friends')->confirmToAddFriend($this->uid, $uid,$add_type);
    }
    /**
     * 获取新朋友及好友列表
     */
    public function dogetMyFriends()
    {
        if(empty($this->uid))
        {
            $_ENV['error']->getErrors(201);
            exit;
        }
        $f_id = common::request('record_id', 'G');
        if($this->vn < 2400)
        {
            $newFriends = $this->load('friends')->getPushFriend($this->uid, empty($f_id));
        }
        else
        {
            $newFriends = array();
        }

        // 干掉小红点
        $this->redis->hdel(REDIS_PREFIX.'IsNewFriends', $this->uid);
        $this->redis->del(REDIS_PREFIX.'FriendPush_'.$this->uid);

        // 好友列表
        $friendsList = $this->load('friends')->getFriendsList($this->uid, $f_id);

        $this->result['result'] = array('newFriends' => $newFriends,'friendsList' =>$friendsList);
    }
    /**
     * 删除好友
     */
    public function dodelFriend()
    {
        $uid = common::request('uid', 'G');
        empty($add_type) && $add_type = 0;
        if(empty($uid))
        {
            $_ENV['error']->getErrors(101);
            exit;
        }
        if(empty($this->uid))
        {
            $_ENV['error']->getErrors(201);
            exit;
        }
        $this->result['result'] = $this->load('friends')->delMyfriend($this->uid, $uid);
    }
    /**
     * 好友排行
     */
    public function dofriendsStandings()
    {
        if(empty($this->uid))
        {
            $_ENV['error']->getErrors(201);
            exit;
        }
        $type = common::request('type', 'G',0);
        $uid = common::request('uid', 'G');
        $offset = common::request('offset', 'G');
        $this->result['result'] = $this->load('friends')->getFriendsMilStandings($this->uid,$type,$uid,$offset);
    }
    /**
     * 获取达人列表
     */
    public function dotalentUsers()
    {
        $this->result['result'] = $this->load('friends')->getTalentUsers();
    }
    /**
     * 获取隐私设置
     */
    public function doprivacy()
    {
        if(empty($this->uid))
        {
            $_ENV['error']->getErrors(201);
            exit;
        }
        $this->result['result'] = $this->load('user')->getPrivacy($this->uid);
    }
    /**
     * 设置隐私
     */
    public function dosetPrivacy()
    {
        if(empty($this->uid))
        {
            $_ENV['error']->getErrors(201);
            exit;
        }
        $userInfo = $this->load('client')->getUserInfo($this->uid);
        if(empty($userInfo))
        {
            $_ENV['error']->getErrors(201);
            exit;
        }
        $gender = common::request('gender', 'G');
        $age = common::request('age', 'G');
        $constellation = common::request('constellation', 'G');
        $occupation = common::request('occupation', 'G');
        $location = common::request('location', 'G');

        $gender = !empty($gender) ? 1 : 0;
        $age = !empty($age) ? 1 : 0;
        $constellation = !empty($constellation) ? 1 : 0;
        $occupation = !empty($occupation) ? 1 : 0;
        $location = !empty($location) ? 1 : 0;
        $privacy = compact('gender','age','constellation','occupation','location');

        $this->result['result'] = $this->load('user')->setPrivacy($this->uid, $privacy);
    }
    /**
     * 推荐好友列表
     */
    public function dorecommendFriendList()
    {
        $uid = $this->uid;
        if(empty($uid))
        {
            $_ENV['error']->getErrors(201);
            exit;
        }

        $list = array();
        $recFriendList = $this->load('friends')->getRecommendFriendList($uid, 20);
        if($recFriendList)
        {
            foreach ($recFriendList as $recUid) {
                $userInfo = $_ENV['client']->getUserInfo($recUid);
                if($userInfo)
                {
                    $privacy = $this->load('user')->getPrivacy($recUid);
                    $recUserInfo = array('uid' => $recUid,
                                  'nickname' => $userInfo['nickname'],
                                  'gender' => $userInfo['gender'],
                                  'header' => $userInfo['header'],
                                  'province' => $userInfo['province'],
                                  'city' => $userInfo['city'],
                                  'lights' => $userInfo['lights'],
                                  'total_mileage' => $userInfo['total_mileage'] <= 0 ? '0.00' : $userInfo['total_mileage'],
                                  'status' => $_ENV['friends']->getFriendStatus($uid,$recUid),
                                  'images' => $_ENV['friends']->getRecFriendImg($recUid)
                        );
                    if(!$privacy['location'])
                    {
                        unset($recUserInfo['province'], $recUserInfo['city']);
                    }
                    $list[] = $recUserInfo;
                }
            }
        }
        $this->result['result'] = $list;
    }

    /**
     * 个人中心-我的装备-想要的/拥有的
     */
    public function domyGearList()
    {
        $gearList = array();

        $type = common::request('type', 'G', 0);
        $page = common::request('page', 'G', 1);

        $type = is_numeric($type) ? $type : 0;
        $page = is_numeric($page) ? $page : 0;

        if($this->uid)
        {
            $gearList = $this->load('gear')->getUserGearList($this->uid, $type, $page, 100);
        }

        $this->result['result'] = $gearList;
    }

    /**
     * 个人中心-我的装备-删除想要的/拥有的
     */
    public function dodelMyGearList()
    {
        $result = 0;

        $type = common::request('type', 'P', 0);
        $gearId = common::request('gearId', 'P', 1);

        $type = is_numeric($type) ? $type : 0;
        $gearId = is_numeric($gearId) ? $gearId : 0;

        if($this->uid && $gearId)
        {
            $result = $this->load('gear')->delUserGear($this->uid, $type, $gearId);
        }

        $this->result['result'] = $result ? 1 : 0;
    }

    /* ================  新增用户接口 ================ */

    /**
     * 手机号注册
     */
    public function doregister()
    {
        $code = common::request('code','P');
        $pwd = common::request('pwd','P');
        $phone = common::request('phone','P');
        $nickname = common::request('nickname', 'P', NULL, TRUE);
        $gender = common::request('gender', 'P');
        $height = common::request('height', 'P');
        $weight = common::request('weight', 'P');
        $birth_year = common::request('birth_year','P');
        $birth_month = common::request('birth_month','P');
        $birth_day = common::request('birth_day','P');
        $province = common::request('province','P','');
        $city = common::request('city','P','');
        $packageChannel  = common::request('packageChannel', 'R', '');
        $fromChannel = trim($packageChannel);
        $birthday = $birth_year."-".$birth_month.'-'.$birth_day;

        if(empty($pwd) || empty($code) || empty($phone) || empty($gender) || empty($height) || empty($weight) || empty($birth_year) || empty($nickname)){
            $_ENV['error']->getErrors(101);
            exit;
        }
        if(!$this->_checkPhone($phone)){
            $_ENV['error']->getErrors(1101);
            exit;
        }
        if($this->vn >= 2850){
            $codeRes = $this->_checkCode($phone, $code);
            if($codeRes !== true){
                $_ENV['error']->getErrors($codeRes);
                exit;
            }
        }
        $userinfo = $this->load('user')->getInfoByPhone($phone);
        if($userinfo){
            $_ENV['error']->getErrors(310);
            exit;
        }
        $checkNo = $this->checkNickname($nickname);
        $errorMap = array('-1'=>501,'-2'=>502,'-3'=>503);//错误码映射
        if($checkNo !== 1){
            $_ENV['error']->getErrors($errorMap[$checkNo]);
            exit;
        }
        $header = '';
        if(isset($_FILES['header']))
        {
            $imgName = md5(common::getHashStr(6).$this->time);

            $newHeaderSource = $this->load('qiniu')->uploadQiNiuImg('header', $imgName);

            if($newHeaderSource)
            {
                $newHeader = $this->load('qiniu')->qiniuPersist($newHeaderSource, '?imageMogr2/thumbnail/180x180');
                $newHeader && $header = $newHeader;
            }
        }
        $pwd = md5(trim($pwd));
        $age = date("Y") - $birth_year;
        $data = compact('phone','pwd','nickname','gender','height','weight','header','fromChannel','birthday','age','province','city');
        $user_data = $this->load('social')->bindUser($phone, '', '', 6, '', $data);
        $this->load('user')->updateStatus($phone,1);
        $this->_saveVerifyTime($phone, $code);
        $this->result['result'] = $user_data;
        $this->result['is_old'] = empty($user_data['total_elapsedtime']) ? 0 : 1;

    }

    /**
     * 用户登录
     */
    public function dologin(){
        $phone = common::request('phone','P');
        $pwd = common::request('pwd','P');
        if(empty($phone) || empty($pwd)){
            $_ENV['error']->getErrors(101);
            exit;
        }
        if(!$this->_checkPhone($phone)){
            $_ENV['error']->getErrors(1101);
            exit;
        }

        $userInfo = $this->load('user')->getInfoByPhone($phone);
        $limitKey = REDIS_PREFIX.'password_limit'.$userInfo['uid'];  //错误次数上限后锁定
        if($this->redis->get($limitKey)){
            $_ENV['error']->getErrors(1005);
            exit;
        }
        if(empty($userInfo)){
            $_ENV['error']->getErrors(309);
            exit;
        }
        if($userInfo['pwd'] !== md5($pwd)){
            $this->_lockLogin($userInfo['uid']);
            $_ENV['error']->getErrors(304);
            exit;
        }
        //生成token
        $time = time();
        $tokenStr = md5($userInfo['uid'] . $time . $this->config['tokenStr']);
        $uid = $this->alphaId->getHash($userInfo['uid']);
        $tokenArr = array($uid, $time, $tokenStr, md5($this->client));
        $token = implode('-', $tokenArr);
        setcookie('joggers_u', $token, time() + 864000 * 365 * 10, '/', '.hupu.com', FALSE, TRUE);
        setcookie('joggers_u', $token, time() + 864000 * 365 * 10, '/', '.ijoggers.com', FALSE, TRUE);
        $userInfo = $this->load('client')->getUserInfo($userInfo['uid']);
        $userInfo['token'] = $token;
        $this->result['is_old'] = empty($user_data['total_elapsedtime']) ? 0 : 1;;
        $this->result['result'] = $userInfo;

    }

    /**
     * 手机号绑定
     */
    public function dobindPhone(){
        $phone = common::request('phone','G');
        $code = common::request('code','G');
        $pwd = common::request('pwd','G');
        $packageChannel  = common::request('packageChannel', 'R', '');
        $fromChannel = trim($packageChannel);
        if(empty($this->uid))
        {
            $_ENV['error']->getErrors(201);
            exit;
        }

        if(empty($phone) || empty($code) || empty($pwd)){
            $_ENV['error']->getErrors(101);
            exit;
        }
        if(!$this->_checkPhone($phone)){
            $_ENV['error']->getErrors(1101);
            exit;
        }
        $codeRes = $this->_checkCode($phone, $code);
        if($codeRes !== true){
            $_ENV['error']->getErrors($codeRes);
            exit;
        }

        $userinfo = $this->load('user')->getInfoByPhone($phone);
        if(!empty($userinfo)){
            $_ENV['error']->getErrors(310);
            exit;
        }
        $pwd = md5($pwd);
        $bindInfo = compact("phone", 'pwd', 'fromChannel');
        $this->load('social')->bindUser($phone, '', '', 6, 0, $bindInfo);
        $this->load('user')->updateStatus($phone,1);
        $this->_saveVerifyTime($phone, $code);
        $this->result['result']['is_success'] = 1;
    }


    /**
     * 获取验证码
     */
    public function dogetCode(){
        $phone = common::request('phone','G');
        $flag = common::request('flag',"G",0);//密码找回1，更改手机、注册和手机绑定改值为0
        if(!$this->_checkPhone($phone)){
            $_ENV['error']->getErrors(1101);
            exit;
        }
        if(!in_array($flag,array(0,1))){
            $_ENV['error']->getErrors(101);
            exit;
        }
        $userinfo = $this->load('user')->getInfoByPhone($phone);

        if($flag == 1 && empty($userinfo)){
            $_ENV['error']->getErrors(309);
            exit;
        }

        if($flag === 0 && !empty($userinfo)){
            $_ENV['error']->getErrors(310);
            exit;
        }
//        include FILE.'lib/sms.class.php';
//        $sms = new sms();
//        $res = $sms->testinSMS($phone,$this->client, $flag);
        $res = $this->load('verifyCode')->sendCodeBytestinSMS($phone,$flag);
        if($res === -1){
            $_ENV['error']->getErrors(1003);
            exit;
        }
        if($res === -2){
            $_ENV['error']->getErrors(1005);
            exit;
        }
        if($res == 3008){
            $_ENV['error']->getErrors(3008);
            exit;
        }

        if($res != 1000){
            $_ENV['error']->getErrors(100);
            exit;
        }
        $flag == 1 ? $this->_insertSms($phone,1) : $this->_insertSms($phone,2);
        $this->result['result']['is_success'] = 1;
    }

    /**
     * 找回密码
     */
    public function dofindPwd(){
        $code = common::request('code','G');
        $pwd = common::request('pwd','G');
        $phone = common::request('phone','G');

        if(empty($phone) || empty($code) || empty($pwd)){
            $_ENV['error']->getErrors(101);
            exit;
        }
        if(!$this->_checkPhone($phone)){
            $_ENV['error']->getErrors(1101);
            exit;
        }

        $codeRes = $this->_checkCode($phone, $code);
        if($codeRes !== true){
            $_ENV['error']->getErrors($codeRes);
            exit;
        }

        $userInfo = $this->load('user')->getInfoByPhone($phone);
        if(empty($userInfo)){
            $_ENV['error']->getErrors(309);
            exit;
        }

        $pwd = md5($pwd);
        $this->load('client')->setUserInfo($userInfo['uid'], array('pwd'=>$pwd));
        $this->_saveVerifyTime($phone, $code);
        $this->redis->del(REDIS_PREFIX.'password_limit'.$userInfo['uid']);
        $this->result['result']['is_success'] = 1;
    }

    /**
     * 验证用户密码
     */
    public function dovalidPassword(){
        $pwd = common::request('pwd',"G");
        $uid = $this->uid;
        if(empty($uid)){
            $_ENV['error']->getErrors(201);
            exit;
        }
        $limitKey = REDIS_PREFIX.'password_limit'.$uid;  //错误次数上限后锁定
        if($this->redis->get($limitKey)){
            $_ENV['error']->getErrors(1005);
            exit;
        }
        $userInfo = $_ENV['client']->getUserInfo($uid);
        if($userInfo['pwd'] != md5($pwd)){
            $this->_lockLogin($uid);
            $_ENV['error']->getErrors(304);
            exit;
        }
        $this->result['result']['is_success'] = 1;

    }

    /**
     * 更改手机号
     */
    public function dochangePhone(){
        $uid = $this->uid;
        if(empty($uid)){
            $_ENV['error']->getErrors(201);
            exit;
        }
        $phone = common::request('phone','G');
        $code = common::request("code",'G');
        if(!$this->_checkPhone($phone)){
            $_ENV['error']->getErrors(1101);
            exit;
        }

        $codeRes = $this->_checkCode($phone, $code);
        if($codeRes !== true){
            $_ENV['error']->getErrors($codeRes);
            exit;
        }
        $newUserInfo = array('phone'=>$phone);
        $this->result['result']['is_success'] = 0;
        if($this->load('client')->setUserInfo($uid, $newUserInfo)){
            $this->load('user')->updateStatus($phone,1);
            $this->_saveVerifyTime($phone, $code);
            $this->result['result']['is_success'] = 1;
        }


    }

    /**
     * 上传通讯录
     */
    public function doupAddressBook(){
        if(empty($this->uid))
        {
            $_ENV['error']->getErrors(201);
            exit;
        }
        $data = common::request('data','P');
        if(empty($data)){
            $_ENV['error']->getErrors(101);
            exit;
        }
        $data = json_decode($data,true);
        foreach($data as $info){
            $adInfo = $this->load('user')->getInfo($this->client,$info['phone']);
            if(!empty($adInfo) || !$this->_checkPhone($info['phone'])){
                continue;
            }
            $userInfo = $_ENV['user']->getInfoByPhone($info['phone']);
            $status = 2;
            if(!empty($userInfo)){
                $status = 1;
            }
            $insertData = $info;
            $insertData['clientid'] = $this->client;
            $insertData['status'] = $status;
            $this->db->insertRowTable('r_address_book', $insertData);
        }
        $this->result['result']['is_success'] = 1;

    }


    /**
     * 通讯录列表
     */
    public function doaddressBookList(){
        $uid = $this->uid;
        if(empty($uid)){
            $_ENV['error']->getErrors(201);
            exit;
        }
        $type = common::request('type','G',1);
        $page = common::request('page','G',1);
        $types = array(1,2);
        if(!in_array($type,$types)){
            $_ENV['error']->getErrors(101);
            exit;
        }
        $user = $_ENV['client']->getUserInfo($uid);
        $phone = isset($user['phone']) ? $user['phone'] : 0;
        if($type == 1){
            $row = 10;
            $uids = $this->_getMyFriendUid($uid);
            $info = $this->load('user')->getPhonesByCid($this->client,$phone,$uids,1,$page,$row);

        }else{
            $row = 15;
            $info = $this->load('user')->getPhonesByCid($this->client,$phone,'',2,$page,$row);
        }

        $this->result['result']['info'] = $info;

    }

    /**
     * 搜索可邀请用户
     */
    public function dosearch(){
        if(!$this->uid){
            $_ENV['error']->getErrors(201);
            exit;
        }
        $name = common::request('name','G');
        $name = trim($name);
        $client = $this->client;
        //$sql = "select name,phone from r_address_book where clientid='$client' and status=2 and name like '%{$name}%'";
        //$result = $this->db->getAll($sql);
        $this->result['result']['invite'] = [];
    }

    /**
     * 信息校验
     */
    public function docheckInfo(){
        $phone = common::request('phone','G');
        $code = common::request('code','G');
        $pwd = common::request('pwd','G');
        if(empty($phone) || empty($code) || empty($pwd)){
            $_ENV['error']->getErrors(101);
            exit;
        }
        if(!$this->_checkPhone($phone)){
            $_ENV['error']->getErrors(1101);
            exit;
        }

        $codeRes = $this->_checkCode($phone, $code);
        if($codeRes !== true){
            $_ENV['error']->getErrors($codeRes);
            exit;
        }
        $this->_saveVerifyTime($phone, $code);
        $this->result['result']['is_success'] = 1;

    }

    /**
     * 批量添加好友
     */
    public function dobatchAddFriend(){
        $uids = common::request('uids', 'G');
        $add_type = common::request('add_type', 'G');
        empty($add_type) && $add_type = 0;
        if(empty($this->uid))
        {
            $_ENV['error']->getErrors(201);
            exit;
        }
        if(empty($uids)){
            $_ENV['error']->getErrors(101);
            exit;
        }
        $uids = json_decode($uids,true);
        foreach($uids as $uid){
            if(!empty($uid) && $uid != $this->uid){
                $this->load('friends')->requestAddFriend($this->uid, $uid,$add_type);
            }
        }
        $this->result['result']['is_success'] = 1;
    }

    /**
     * 检测用户/设备是否被禁止是否可以聊天
     */
    public function doisChat(){
        $uid = $this->uid;
        if(empty($uid))
        {
            $_ENV['error']->getErrors(201);
            exit;
        }
        $this->result['result']['isChat'] = $this->load('user')->checkChatIsForbid();
    }

    /**
     * 按日期记录用户体重数据 (当天最后一次体重+同步更新用户信息中的体重)
     */
    public function doaddUserWeightData() {
        if(!$this->uid) //用户未登录
        {
            $_ENV['error']->getErrors(201);
            exit;
        }
        $uid = (int)common::request('uid','P','');
        $weight = (double)common::request('weight','P');
        if (!$uid || $this->uid != $uid || !$weight) {
            $_ENV['error']->getErrors('101');
            exit;
        }
        $result = $this->load('user')->addUserWeightData($uid,$weight);
        //$this->load('log')->writeLog('api','addUserWeightData:'.date('Y-m-d H:i:s').' result:'.var_export($result,true).PHP_EOL,'print-'.date('Y-m-d').'.log');
        //更新用户主表数据
        $re = $this->load('user')->updateUserWeight($uid,$weight);
        $this->result['result']['is_success'] = 1;
    }

    /**
     * 查询七天按天分组的用户体重数据  （体重+消耗的卡路里总数/天，外加减肥求监督话题参与总人数）
     */
    public function dogetWeightDataForChart() {
        if(!$this->uid) //用户未登录
        {
            $_ENV['error']->getErrors(201);
            exit;
        }
        $uid = (int)common::request('uid','P',0);
        if (!$uid || $this->uid != $uid) {
            $_ENV['error']->getErrors('101');
            exit;
        }
        $endTime = (int)common::request('endTime','P',date('Ymd'));
        if ($endTime && strlen($endTime) <> 8)  {
            $_ENV['error']->getErrors('101');
            exit;
        }
        $result = $this->load('user')->getWeightDataForChart($uid,$endTime);
        $this->result['result']['listCharts']= $result;
        //查询减肥求监督话题参与总人数
        $num = $this->load('topic')->getTopicUsersNum('#减肥求监督#');
        $this->result['result']['weightNum'] = $num;
        //用户目标体重
        $this->result['result']['targetWeight'] = (float)$this->redis->hget(REDIS_PREFIX . 'UserInfo_' . $uid,'target_weight');
    }

    /**
     * 设置用户目标体重
     */
    public function dosetUserTargetWeight() {
        $uid = $this->uid;
        if (!$uid) {
            $_ENV['error']->getErrors(201);
            exit;
        }
        $targetWeight = common::request('targetWeight', 'P', 0);
        $this->result['result']['is_success'] = 0;
        if ($this->load('client')->setUserInfo($uid, array('target_weight' => $targetWeight))) {
            $this->result['result']['is_success'] = 1;
        }
    }







    /**
     * 手机号码验证
     * @param $phone
     * @return bool
     */
    private function _checkPhone($phone)
    {
        $result = FALSE;

        if(!empty($phone) && preg_match('/^1\d{10}$/', $phone))
        {
            $phoneFix = array('190','133', '153', '180', '181', '189', '1349', '130', '131', '132', '155', '156', '185', '186', '134', '135', '136', '137', '138', '139', '147', '150', '151', '152', '157', '158','159', '170', '176', '178', '177', '182', '187', '188', '183', '184', '145', '1709', '154', '165');

            foreach ($phoneFix as $pk => $pv)
            {
                if(strpos($phone, $pv) !== FALSE)
                {
                    $result = TRUE;
                    break;
                }
            }
        }

        return $result;
    }

    /**
     * 校验验证码
     * @param $code
     * @return bool
     */
//    private function _checkCode($code){
//        include FILE.'lib/sms.class.php';
//        $sms = new sms();
//        $res = $sms->chekcode($code);
//        return $res;
//    }
    private function _checkCode($mobile, $code){
        return $this->load('verifyCode')->checkCode($mobile, $code);
    }

    /**
     * 操作成功后，，更新验证码表 ，删除redis缓存
     */
    private function _saveVerifyTime($mobile, $code) {
        return $this->load('verifyCode')->saveVerifyTime($mobile, $code);
    }

    /**
     * @param $uid
     */
    private function _lockLogin($uid){
        $countKey = REDIS_PREFIX.'password_count'.$uid;  //一小时错误次数
        $limitKey = REDIS_PREFIX.'password_limit'.$uid;  //错误次数上限后锁定
        //一小时内错误次数统计
        if($this->redis->exists($countKey)){
            $this->redis->incr($countKey);
        }else{
            $this->redis->setex($countKey,3600,1);
        }
        //错误次数达到限制后锁定10分钟,并且删除次数累计值
        if($this->redis->get($countKey) >= 5) {
            $this->redis->setex($limitKey,600,1);
            $this->redis->del($countKey);
        }
    }

    /**
     * 获取好友UID
     * @param $uid
     * @return array
     */
    private function _getMyFriendUid($uid){
        $res = $this->load('friends')->getFriendsList($uid);
        $uids = array();
        if(!empty($res)){
            foreach($res as $item){
                $uids[] = $item['uid'];
            }
        }
        return $uids;
    }

    private function _insertSms($phone,$type){
        $insertData = array('phone'=>$phone,'addtime'=>date("Y-m-d"),'type'=>$type);
        return $this->db->insertRowTable('r_sms', $insertData);
    }
    
    /*
     * 设置是否同步消息到跑友秀
     */
    public function dosetSyncRunMoodPost(){
        $sync = common::request('sync','P',0);
        $uid = $this->uid;
        
        if(empty($uid))
        {
            $_ENV['error']->getErrors(201);
            exit;
        }
        if(!isset($sync)){
            $_ENV['error']->getErrors(101);
            exit;
        }
        
        $sync = empty($sync) ? 0 : 1;
        $this->load('user')->setUserExt($uid,['sync_run_mood_post'=>$sync]);
        
    }
    
    /*
     * 获得百川 openimuser
     */
    public function dogetBaichuanImUser(){
        $uid = $this->uid;
        if(empty($uid))
        {
            $_ENV['error']->getErrors(201);
            exit;
        }
        $userInfo = $this->load('client')->getUserInfo($uid);
        
        $salt = $this->config['app_key'];
        $ali_mod = $this->load('alibaba');
        
        $row = [
            'nick' => $userInfo['nickname'],
            'icon_url' => $userInfo['header'],
            'userid' => 'bc'.$uid,
            'password' => md5($uid.$salt.'password'),
            'email' => '',
            'mobile' => '',
        ];
        $ali_mod->addOpenImUser($row);
        $openimuser = [
            //'userid' => md5($uid.$salt),
            'userid' => 'bc'.$uid,
            'password' => md5($uid.$salt.'password')
        ];
        $this->result['result'] = $openimuser;
    }
}